import java.util.*;

public class GamePlay {
	
	static int [] lineCounts = new int[8];
	
	public static void main(String[] args) {
		GameBoard board = new GameBoard();
		boolean isValidMove = true;
		board.createNew();
		Scanner askUserInput = new Scanner(System.in);
		
		/*
		 CODE BELOW IS FOR WHEN PLAYING AGAINST A COMPUTER
		 
		 listHanded = the list that is given by the other computer
		  
		 while(!board.done(board) && board.win(board) == 0) {
		 	board.mimicList(listHanded);
		 	getNextMove(board.returnBoard());
		  		
		 	board.toString(board.returnBoard());
		  
		 	if(board.win(board) == -1) {
				System.out.println("*******************************");
				System.out.println("Team Tom and Marty has won");
				System.out.println("*******************************");
			}
			else if(board.win(board) == 1){
				System.out.println("*******************************");
				System.out.println("Team Sarah and Kamil has won");
				System.out.println("*******************************");
			}
		}
		*/
		
		//most of this needs to be removed to play against another computer
		while(!board.done(board) && board.win(board) == 0) {
			isValidMove = false;
			int row = 0;
			int column = 0;
			while(!isValidMove) {
				System.out.print("Which row would you like to place your mark at:  ");
				row = askUserInput.nextInt() -1;
				System.out.print("Which column would you like to place your mark at:  ");
				column = askUserInput.nextInt() -1;
				if((row < 0 || row > 2) || (column < 0 || column > 2)) {
					isValidMove = false;
					System.out.println();
					System.out.println("Please enter a row and column number between one and three");
					System.out.println();
				}
				else {
					isValidMove = true;
				}
			}
			if(board.getPiece(row, column) != 0) isValidMove = false;
			
			for(int g = 0; g < 50; g++) {
				System.out.println();
			}
			if(isValidMove) {
				board.makeOpponentMove(row, column, board);
				getNextMove(board);
			}
			else {
				isValidMove = false;
				System.out.println("------------------------------------------");
				System.out.println("That is not a valid move, please try again");
				System.out.println("------------------------------------------");
			}
			board.toString(board.returnBoard());
			if(board.win(board) == -1) {
				System.out.println("*******************************");
				System.out.println("Team Tom and Marty has won");
				System.out.println("*******************************");
			}
			else if(board.win(board) == 1){
				System.out.println("*******************************");
				System.out.println("Team Sarah and Kamil has won");
				System.out.println("*******************************");
			}
		}
	}
	
	/*
	 * lineCount definitions here:
	 * [0] = row 1;  [1] = row 2;  [2] = row3;
	 * [3] = column 1; [4] = column 2;  [5] = column 3;
	 * [6] = diagonal right to left;  [7] = diagonal left to right;
	 */
	public static void getNextMove(GameBoard board) {
		boolean makeStrategicMove = true;
		
		if(board.win(board) == 0) {
			
			lineCounts[0] = board.getPiece(0,0) + board.getPiece(0,1) + board.getPiece(0,2); //row one
			lineCounts[1] = board.getPiece(1,0) + board.getPiece(1,1) + board.getPiece(1,2); //row two
			lineCounts[2] = board.getPiece(2,0) + board.getPiece(2,1) + board.getPiece(2,2); //row three
			lineCounts[3] = board.getPiece(0,0) + board.getPiece(1,0) + board.getPiece(2,0); //column one
			lineCounts[4] = board.getPiece(0,1) + board.getPiece(1,1) + board.getPiece(2,1); //column two
			lineCounts[5] = board.getPiece(0,2) + board.getPiece(1,2) + board.getPiece(2,2); //column three
			lineCounts[6] = board.getPiece(0,0) + board.getPiece(1,1) + board.getPiece(2,2); //diagonal down, left to right
			lineCounts[7] = board.getPiece(0,2) + board.getPiece(1,1) + board.getPiece(2,0); //diagonal down, right to left
			boolean madeMove = false;
			
			for(int h = 0; h < 8; h++) {
				if(lineCounts[h] == -2  && !madeMove) {
					//this means we place a piece that will win the game
					addInto(h, board);
					makeStrategicMove = false;
					madeMove = true;
					break;
				}
			}
			for(int k = 0; k < 8; k++) {
				if(lineCounts[k] == 2 && !madeMove) {
					//do the move here, because this is the place that needs to be played
					addInto(k, board);
					makeStrategicMove = false;
					madeMove = true;
					break;
				}
			}
			if(makeStrategicMove) { makeStrategicNextMove(board); }
		}
	}
	
	/**
	 * Takes in a single number (1-7) that represents a row, column, or diagonal, according to
	 * the number assignments in the getNextMove method:
	 * [0] = row 1;  [1] = row 2;  [2] = row3;
	 * [3] = column 1; [4] = column 2;  [5] = column 3;
	 * [6] = diagonal right to left;  [7] = diagonal left to right;
	 * 
	 * It then decided which spot in that line is empty, and it then makes the move
	 */
	public static void addInto(int lineCountRef, GameBoard board) {
		if(lineCountRef == 0){
            if(board.getPiece(0,0)==0){
                board.makeMove(0,0,board);
            }
            else if(board.getPiece(0,1)==0){
                board.makeMove(0,1,board);
            }
            else{
                board.makeMove(0,2,board);
            }
        }
        else if(lineCountRef == 1){
            if(board.getPiece(1,0)==0){
                board.makeMove(1,0,board);
            }
            else if(board.getPiece(1,1)==0){
                board.makeMove(1,1,board);
            }
            else{
                board.makeMove(1,2,board);
            }
        }
        else if(lineCountRef == 2){
            if(board.getPiece(2,0)==0){
                board.makeMove(2,0,board);
            }
            else if(board.getPiece(2,1)==0){
                board.makeMove(2,1,board);
            }
            else{
                board.makeMove(2,2,board);
            }
        }
        else if(lineCountRef == 3){
            if(board.getPiece(0,0)==0){
                board.makeMove(0,0,board);
            }
            else if(board.getPiece(1,0)==0){
                board.makeMove(1,0,board);
            }
            else{
                board.makeMove(2,0,board);
            }
        }
        else if(lineCountRef == 4){
            if(board.getPiece(0,1)==0){
                board.makeMove(0,1,board);
            }
            else if(board.getPiece(1,1)==0){
                board.makeMove(1,1,board);
            }
            else{
                board.makeMove(2,1,board);
            }
        }
        else if(lineCountRef == 5){
            if(board.getPiece(0,2)==0){
                board.makeMove(0,2,board);
            }
            else if(board.getPiece(1,2)==0){
                board.makeMove(1,2,board);
            }
            else{
                board.makeMove(2,2,board);
            }
        }
        else if(lineCountRef == 6){
            if(board.getPiece(0,0)==0){
                board.makeMove(0,0,board);
            }
            else if(board.getPiece(1,1)==0){
                board.makeMove(1,1,board);
            }
            else{
                board.makeMove(2,2,board);
            }
        }
        else if(lineCountRef == 7){
            if(board.getPiece(0,2)==0){
                board.makeMove(0,2,board);
            }
            else if(board.getPiece(1,1)==0){
                board.makeMove(1,1,board);
            }
            else{
                board.makeMove(2,0,board);
            }
        }
	}
	
	public static void makeStrategicNextMove(GameBoard board) {
		int firstLine = 0;
		int secondLine = 0;
		
		for(int i = 0; i < 8; i++) {
			if(lineCounts[i] == -1) {
				firstLine = i;
				for(int j = 0; j < 8; j++) {
					if(lineCounts[j] == -1 && j != firstLine) { secondLine = j; }
				}
			}
		}
		
		if(lineCounts[firstLine] == 0 && lineCounts[secondLine] == 3) { //row one and column one
			board.makeMove(0, 0, board);
		}
		else if(lineCounts[firstLine] == 0 && lineCounts[secondLine] == 4) { //row one and column two
			board.makeMove(0, 1, board);
		}
		else if(lineCounts[firstLine] == 0 && lineCounts[secondLine] == 5) { //row one and column three
			board.makeMove(0, 2, board);
		}
		else if(lineCounts[firstLine] == 1 && lineCounts[secondLine] == 3) { //row two and column one
			board.makeMove(1, 0, board);
		}
		else if(lineCounts[firstLine] == 1 && lineCounts[secondLine] == 4) { //row two and column two
			board.makeMove(1, 1, board);
		}
		else if(lineCounts[firstLine] == 1 && lineCounts[secondLine] == 5) { //row two and column three
			board.makeMove(1, 2, board);
		}
		else if(lineCounts[firstLine] == 2 && lineCounts[secondLine] == 3) { //row three and column one
			board.makeMove(2, 0, board);
		}
		else if(lineCounts[firstLine] == 2 && lineCounts[secondLine] == 4) { //row three and column two
			board.makeMove(2, 1, board);
		}
		else if(lineCounts[firstLine] == 2 && lineCounts[secondLine] == 5) { //row three and column three
			board.makeMove(2, 2, board);
		}
		else {
			if(board.getPiece(0,0) == 1 && board.getPiece(2,2) == 0)
				board.makeMove(2, 2, board);
			
			else if(board.getPiece(0,2) == 1 && board.getPiece(2,0) == 0)
				board.makeMove(2, 0, board);
			
			else if(board.getPiece(2,0) == 1 && board.getPiece(0,2) == 0)
				board.makeMove(0, 2, board);
				
			else if(board.getPiece(2,2) == 1 && board.getPiece(0,0) == 0)
				board.makeMove(0, 0, board);
			
			else if(board.getPiece(1, 1) == 0) board.makeMove(1, 1, board);
			else if(board.getPiece(1, 2) == 0) board.makeMove(1, 2, board);
			else if(board.getPiece(0, 1) == 0) board.makeMove(0, 1, board);
			else if(board.getPiece(1, 0) == 0) board.makeMove(1, 0, board);
			else if(board.getPiece(2, 1) == 0) board.makeMove(2, 1, board);
			
			}
	}
	
	
}
